package com.springcloud.sc.ucpv5.tree.data;

import com.springcloud.sc.ucp.v5.api.entity.UcpUserDetailInfo;
import com.springcloud.sc.ucp.v5.api.vo.UserStatusInfo;
import com.springcloud.sc.ucpv5.client.commondata.ModelBase;
import com.springcloud.sc.ucpv5.client.commondata.MyGroup;
import com.springcloud.sc.ucpv5.client.commondata.MyNode;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component(value = "ucpDataUtil")
public class UcpDataUtil {

    @Resource
    private RedisTemplate redisTemplate;

    private static List<Map<String,Object>> pageList = new ArrayList<>();

    public static void setList( Map<String,Object> map){
        pageList.add( map );
    }
    public static void clearList(){
        pageList.clear();
    }
    public static List getList(){
        return pageList;
    }

    public boolean findPageByNode(MyNode node,String searchName, boolean withChild){
        Map<String,Object> map = new HashMap<>();
        map = new HashMap<String,Object>();
        map.put("nodeName",node.getIdlData().NodeName);
        map.put("nodeNumber",node.getIdlData().NodeNumber);
        map.put("type","Node");
        map.put("typeChild",node.getIdlData().getNodeType() );

        if(searchName == null || searchName.equals("") || searchName.equals("null")){
            setList( map );
        }else{
            if(node.getIdlData().getNodeName().indexOf(searchName) > 0){
                setList( map );
            }
        }

        if (withChild) {
            for (MyNode node_children : node.getListModel().getNodeList()) {
                //只找一层节点
                findPageByNode(node_children , searchName , false );
            }
            for (MyGroup group_children : node.getListModel().getGroupList()) {
                findPageByGroup(group_children , searchName , false , false , null );
            }
            for (MyUser user_children : node.getListModel().getUserList()) {
                findPageByUser(node, user_children , searchName , withChild);
            }
        }
        return true;
    }
    public boolean findPageByGroup(MyGroup group, String searchName , boolean withChild , boolean withUserChild , MyGroup parentGroup){
        Map<String,Object> map = new HashMap<>();
        if(group!=null && group.getIdlData()!=null && group.getIdlData().getGroupTelno()!=null){
            map.put("nodeName",group.getIdlData().GroupName);
            map.put("nodeNumber",group.getIdlData().GroupTelno);
            map.put("type","Group");
            map.put("typeChild",group.getIdlData().getGroupType() );
        }
        if(searchName == null || searchName.equals("") || searchName.equals("null")){
            setList( map );
        }else{
            if(group.getIdlData().getGroupName().indexOf(searchName) > 0){
                setList( map );
            }
        }
        if(!group.getListModel().getGroupList().isEmpty()){
            for (MyGroup group_children : group.getListModel().getGroupList()) {
                findPageByGroup(group_children ,searchName, withChild , withUserChild , parentGroup);
            }
        }

        //只显示本组用户，不显示子组用户
        if (withUserChild == true ){
            if(parentGroup.getIdlData().GroupTelno == group.getIdlData().getGroupTelno()){
                withUserChild = false;
                for (MyUser user_children : group.getListModel().getUserList()) {
                    findPageByUser(group, user_children , searchName , withChild);
                }
            }
        }
        return true;
    }

    public void findPageByUser(ModelBase parent, MyUser user , String searchName , boolean withChild ){
        Map<String,Object> tempUserMap = new HashMap<String,Object>();
        tempUserMap.put("nodeName",user.getIdlData().getDesc());
        tempUserMap.put("nodeNumber",user.getIdlData().getUserTelno());
        tempUserMap.put("type","User");
        int userType = user.getIdlData().getUserType() ;
        int userTypeData = userType & 0x000000FF;
        int tmp = (userType >> 10) & 0x0000000F;
        userType = userTypeData * 10 + tmp;
        tempUserMap.put("typeChild", userType );

        if(parent instanceof MyGroup){
            // wsy 20190603 增加一个属性, 用户是否具有语音发布权限
            String grouNumber = ((MyGroup)parent).getIdlData().getGroupTelno();
            tempUserMap.put("VoiceAuthority", user.checkVoiceAuthorityGroup(grouNumber));
        }


        UcpUserDetailInfo userDetailInfo = null;
        //系统启动时设置格式是如此 。StartCommandRunner.java
        Object redisObj = redisTemplate.opsForValue().get( "online."+user.getIdlData().getUserTelno() );
        if(redisObj!=null){
            userDetailInfo = (UcpUserDetailInfo)redisObj;
        }
        if(userDetailInfo!=null){
            UserStatusInfo statusInfo = getSimpleUserStatusInfo( userDetailInfo );
            tempUserMap.put("statusInfo" , statusInfo);
        }


        if(searchName == null || searchName.equals("") || searchName.equals("null") ){
            setList( tempUserMap );
        }else{
            if(user.getIdlData().UserName.indexOf( searchName ) > 0 ){
                setList( tempUserMap );
            }
        }
    }

    public WebTreeNode nodeGetNode(MyNode node, boolean withChild){
        WebTreeNode webTreeNode = new WebTreeNode();
        try{
            webTreeNode.setNodeNumber( node.getIdlData().getNodeNumber() );
            webTreeNode.setName( node.getIdlData().getNodeName() );
            webTreeNode.setParentId( node.getIdlData().getParentNumber() );
            webTreeNode.setPhysicsNodeNumber( node.getIdlData().getPhysics_NodeNumber() );
            webTreeNode.setId( node.getIdlData().getNodeNumber() );
            webTreeNode.setType( "node" );
            webTreeNode.setTypeChild( node.getIdlData().getNodeType() );
        }catch (Exception e) {
            e.printStackTrace();
        }
        if (withChild) {
            for (MyNode node_children : node.getListModel().getNodeList()) {
                webTreeNode.add(nodeGetNode(node_children, withChild));
            }
            for (MyGroup group_children : node.getListModel().getGroupList()) {
                webTreeNode.add(nodeGetGroup(group_children, withChild));
            }
            for (MyUser user_children : node.getListModel().getUserList()) {
                webTreeNode.add(userGetDetailUser(user_children));
            }
        }
        return webTreeNode;
    }

    public WebTreeNode nodeGetGroup(MyGroup group, boolean withChild) {
        WebTreeNode groupNode = new WebTreeNode();
        try{
            groupNode.setNodeNumber(  group.getIdlData().getGroupTelno() );
            groupNode.setName( group.getIdlData().getGroupName() );
//            groupNode.setName( group.getIdlData().getGroupName() + "["+ group.getIdlData().getGroupTelno()+"]" );   //备份
            groupNode.setParentId( group.getIdlData().getParGrpTelno() );
            groupNode.setGrpList( group.getIdlData().getParGrpTelno() );
            groupNode.setPhysicsNodeNumber(  group.getPhysicsNodeNumber() );
            groupNode.setId(  group.getIdlData().getGroupTelno() );
            groupNode.setType( "group" );
            groupNode.setTypeChild( group.getIdlData().getGroupType() );
            groupNode.setPriority( group.getIdlData().getPriority() );
        }catch (Exception e) {
            e.printStackTrace();
        }
        if (withChild) {
            for (MyGroup group_children : group.getListModel().getGroupList()) {
                groupNode.add(nodeGetGroup(group_children, withChild));
            }
            for (MyUser user_children : group.getListModel().getUserList()) {
                groupNode.add(userGetDetailUser(user_children));
            }
        }
        return groupNode;
    }

    public WebTreeNode userGetDetailUser(MyUser user) {
        WebTreeNode userTreeNode = new WebTreeNode();
        userTreeNode.setNodeNumber( user.getIdlData().getUserTelno() );
//        userTreeNode.setName( user.getIdlData().getDesc() +"["+user.getIdlData().getUserTelno()+"]" );
        userTreeNode.setName( user.getIdlData().getDesc()  );
        userTreeNode.setParentId( user.getIdlData().getGrpList() );
        userTreeNode.setPhysicsNodeNumber( user.getPhysicsNodeNumber() );
        userTreeNode.setId( user.getIdlData().getUserTelno() );
        userTreeNode.setType( "user" );

        int userType = user.getIdlData().getUserType() ;
        int userTypeData = userType & 0x000000FF;
        int tmp = (userType >> 10) & 0x0000000F;
        userType = userTypeData * 10 + tmp;
        userTreeNode.setTypeChild( userType );
        userTreeNode.setPriority( user.getIdlData().getPriority() );

        UcpUserDetailInfo userDetailInfo = null;
        Object redisObj = redisTemplate.opsForValue().get("online."+user.getIdlData().getUserTelno());
        if(redisObj!=null){
            userDetailInfo = (UcpUserDetailInfo)redisObj;
        }
        if(userDetailInfo!=null){
            UserStatusInfo statusInfo = getSimpleUserStatusInfo( userDetailInfo );
            userTreeNode.setStatusInfo( statusInfo );
        }
        return userTreeNode;
    }

    public UserStatusInfo getSimpleUserStatusInfo(UcpUserDetailInfo user) {
        UserStatusInfo userStatusInfo = new UserStatusInfo();
        userStatusInfo.setLongitude( user.getLongitude()!=null ? user.getLongitude() : 0  );
        userStatusInfo.setLatitude( user.getLatitude() != null ? user.getLatitude() :0 );
        userStatusInfo.setSpeed( user.getSpeed()!=null ? user.getSpeed() : 0 );
        userStatusInfo.setDirection( user.getDirection() !=null ? user.getDirection() : 0 );
        userStatusInfo.setHeight( user.getHeight() !=null ? user.getHeight() : 0 );
        userStatusInfo.setStartTime( user.getStartTime() );
        userStatusInfo.setEndTime( user.getEndTime() );
        userStatusInfo.setUserStatus( user.getUserStatus() );
        userStatusInfo.setUserCallStatus( user.getUserCallStatus() );
        return userStatusInfo;
    }

    public List<WebTreeNode> nodeGetOwnGroup(MyNode node, MyUser user){
        List<WebTreeNode> treeNodeList = new ArrayList<WebTreeNode>();
        List<MyGroup> groupList = user.getGroupList();
        groupList.forEach(group_children->{
            treeNodeList.add(nodeGetGroup(group_children, true));
        });
        return treeNodeList;
    }
}
